package xin.ctkqiang.controller;

import java.util.Scanner;

public class UserUtil {
    private final Scanner scanner;

    public UserUtil(Scanner scanner) {
        this.scanner = scanner;
    }

    public int askForPositiveInt(String prompt) {
        final int DEFAULT = ExploitDbController.PAGE_SIZE;

        int result = DEFAULT;

        while (true) {
            System.out.print(prompt + " (默认 " + DEFAULT + "): 🌼 ");
            String input = scanner.nextLine().trim();

            if (input.isEmpty()) {
                System.out.println("使用默认值：" + DEFAULT);
                return DEFAULT;
            }

            try {
                result = Integer.parseInt(input);
                if (result > 0) {
                    return result;
                } else {
                    return DEFAULT;
                }
            } catch (NumberFormatException e) {
                System.out.println("无效输入，请输入一个有效的整数！");
            }
        }
    }

    public boolean askForYesNo(String question) {
        while (true) {
            System.out.print(question + " (y/n): ");
            String input = scanner.nextLine().trim().toLowerCase();
            if ("y".equals(input)) {
                return true;
            } else if ("n".equals(input)) {
                return false;
            } else {
                System.out.println("请输入 y 或 n 哦~");
            }
        }
    }

    public String askForNonEmptyString(String question) {
        while (true) {
            System.out.print(question + " ");
            String input = scanner.nextLine().trim();

            if (!input.isEmpty()) {
                return input;
            }

            System.out.println("输入不能为空，请重新输入。");
        }
    }

}
